package com.neutech.solar.entity;

import com.neutech.solar.constant.Constant;
import com.neutech.solar.util.ImageUtil;

import java.awt.*;
import java.util.Random;

public class Planet extends Star{

    public Star star;

    public Planet(){

    }
    public Planet(Star star,String imageKey,double AU,double e,int T,String name){
        this.star = star;
        this.img = ImageUtil.maps.get(imageKey);
        this.width = img.getWidth(null);
        this.height = img.getHeight(null);
        this.longAxis = getLongAxisByAU(AU);
        this.shortAxis = getShortAxisByE(e);
        this.x = Constant.WINDOW_WIDTH / 2 + longAxis - this.width / 2;
        this.y = Constant.WINDOW_HEIGHT / 2 - this.height / 2;
        this.speed = getSpeedByT(T);
        this.theta = 0.0;
        this.name = name;

    }
    public Planet(Star star,String imageName,double AU,double e,int T,String name,boolean smallPlanet){
        this(star,imageName,AU,e,T,name);
        this.smallPlanet = smallPlanet;

    }
    public Planet(Star star,String imageName,double AU,double e,int T,String name,boolean smallPlanet,boolean venus){
        this(star,imageName,AU,e,T,name,smallPlanet);
        this.venus = venus;
    }
    public Planet(Star star,String imageName,double AU,double e,int T,String name,boolean smallPlanet,boolean venus,boolean moon){
        this(star,imageName,AU,e,T,name,smallPlanet,venus);
        this.moon = moon;
    }


    private double getSpeedByT(int T){
        return 0.1 * 365 / T;
    }

    private int getLongAxisByAU(double AU){
        return (int) (100 * AU);
    }
    private int getShortAxisByE(double e){
        return (int) (this.longAxis * Math.sqrt(1 - e * e));
    }



    @Override
    public void draw(Graphics g) {
        // 画轨道
        Color c = g.getColor();
        g.setColor(Color.white);
        if (!smallPlanet && !moon){
            this.drawTrace(g);
        }
        // 画图片

        g.drawImage(img,x,y,null);
        // 写名字
        Font f =g.getFont();
        g.setFont(new Font("宋体",Font.BOLD,15));
        g.drawString(name,x + this.width / 2,y);
        g.setFont(f);
        g.setColor(c);



        move();
    }

    //定义画轨道
    private void drawTrace(Graphics g){
        int x = (int)(this.star.x - this.longAxis + star.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
        int y = this.star.y - this.shortAxis + star.height / 2;
        int width = 2 * this.longAxis;
        int height = 2 * this.shortAxis;
        g.drawOval(x,y,width,height);
    }

    @Override
    public void move() {
        if (!venus) {
            x = (int) ((longAxis * Math.sin(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.cos(theta)) + star.y + star.height / 2 - this.height / 2;
        }else {
            x = (int) ((longAxis * Math.cos(theta)) + star.x + star.width / 2 - this.width / 2 + Math.sqrt(longAxis * longAxis - shortAxis * shortAxis));
            y = (int) (shortAxis * Math.sin(theta)) + star.y + star.height / 2 - this.height / 2;
        }
        theta += speed;
    }
}
